package com.jicheng.controller;

import com.jicheng.Utils.ResultMess;
import com.jicheng.dto.data.SyncInfoParams;
import com.jicheng.dto.data.integrity.DelIntegerParams;
import com.jicheng.dto.data.log.LogsInfo;
import com.jicheng.dto.data.log.SyncDetailInfo;
import com.jicheng.dto.data.log.SyncLogsInfo;
import com.jicheng.dto.data.profile.ProfileSyncInfo;
import com.jicheng.dto.data.task.DelParams;
import com.jicheng.dto.data.task.TaskInfo;
import com.jicheng.dto.task.SynTemplLibParams;
import com.jicheng.dto.task.TemplateParams;
import com.jicheng.entity.task.template.TemplateMap;
import com.jicheng.ext.util.UUIDUtils;
import com.jicheng.service.IDataSyncService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(tags = "data-sync（数据同步）")
@RestController
@RequestMapping(value = "data-sync", produces = "application/json;charset=UTF-8")
public class DataSyncController {

    @Autowired
    private IDataSyncService dataSyncService;

    @RequestMapping(value = "/profile/detail", method = RequestMethod.POST)
    @ApiOperation(value = "同步廉政档案详情")
    public ResultMess syncProfileDetail(@RequestBody List<ProfileSyncInfo> params) {
//        if (SecurityUtils.getSubject().getPrincipals()==null){
//            throw new BaseException(ApiResponseCode.未授权);
//        }
//        String userId = SecurityUtils.getSubject().getPrincipals().oneByType(Pac4jPrincipal.class)
//                .getProfile().getAttribute("userId").toString();
//        if ( StringUtils.isEmpty(userId)){
//            throw new BaseException(ApiResponseCode.未授权);
//        }
        dataSyncService.syncProfileDetail(params, UUIDUtils.uuid());

        return new ResultMess(true,"用户照片同步成功！",null);
    }

    @ApiOperation(value = "同步图片")
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    public ResultMess syncPhotos(@RequestParam("file") MultipartFile file) {

       String imgPth =  dataSyncService.syncPhotos(file);
        return new ResultMess(true,"用户照片同步成功！",imgPth);
    }

    @ApiOperation(value = "同步操作日志")
    @RequestMapping(value = "/log", method = RequestMethod.POST)
    public ResultMess syncLogs(@RequestBody LogsInfo params) {
        dataSyncService.syncLogs(params);
        return new ResultMess(true,"操作日志同步成功！");
    }

    @ApiOperation(value = "同步我的任务")
    @RequestMapping(value = "/mission", method = RequestMethod.POST)
    public ResultMess mission(@RequestBody List<TaskInfo> params) {
        dataSyncService.syncTaskDetail(params);
        return new ResultMess(true,"我的任务同步成功！");

    }
    @ApiOperation(value = "同步我的任务-资料库",notes = "type参数代表附件的类型,1代表模板的图片，2代表上传的附件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件数组", required = true, dataType = "MultipartFile", paramType = "query"),
            @ApiImplicitParam(name = "attachmentId", value = "附件id", required = true, dataType = "String", paramType = "query"),
    })
    @RequestMapping(value = "/syncAttachment", method = RequestMethod.POST)
    public ResultMess syncAttachment(@RequestParam("file") MultipartFile file,String attachmentId) {
        dataSyncService.syncAttachment(file,attachmentId);
        return new ResultMess(true,"附件同步成功！");

    }

    @ApiOperation(value = "记录同步数据信息")
    @RequestMapping(value = "/saveSynInfo", method = RequestMethod.POST)
    public ResultMess saveSynInfo(@RequestBody SyncInfoParams syncInfoParams) {
        dataSyncService.saveSynInfo(syncInfoParams);
    return new ResultMess(true,"");
    }

    @ApiOperation(value = "同步删除任务")
    @RequestMapping(value = "/delTask", method = RequestMethod.POST)
    public ResultMess delTask(@RequestBody DelParams delParams) {
        dataSyncService.delTask(delParams);
        return new ResultMess(true,"删除任务成功！");

    }

    @ApiOperation(value = "检查附件是否已经上传")
    @RequestMapping(value = "/checkAttachment", method = RequestMethod.POST)
    public ResultMess checkAttachment(String attachmentId) {
        int isExis = dataSyncService.checkAttachment(attachmentId);
        return new ResultMess(true,"",isExis);

    }

    @ApiOperation(value = "保存上传过的附件信息")
    @RequestMapping(value = "/saveAttachmented", method = RequestMethod.POST)
    public ResultMess saveAttachmented(String[] attachmentId,String taskId) {
        dataSyncService.saveAttachmented(attachmentId,taskId);
        return new ResultMess(true,"保存成功！",null);

    }
    @ApiOperation(value = "同步删除廉政档案")
    @RequestMapping(value = "/delIntegrity", method = RequestMethod.POST)
    public ResultMess delIntegrity(@RequestBody DelIntegerParams delIntegerParams) {
        dataSyncService.delIntegrity(delIntegerParams);
        return new ResultMess(true,"删除成功！",null);

    }
    @ApiOperation(value = "同步模板库")
    @RequestMapping(value = "/syncTemplate", method = RequestMethod.POST)
    public ResultMess syncTemplate(@RequestBody SynTemplLibParams params) {
        dataSyncService.syncTemplate(params);
        return new ResultMess(true,"同步模板库成功！",null);

    }


}
